

package com.jmhy.sdk.view;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.jmhy.sdk.activity.JmCommunityActivity;
import com.jmhy.sdk.activity.View.FloatUserInfoView;
import com.jmhy.sdk.common.JiMiSDK;
import com.jmhy.sdk.config.AppConfig;
import com.jmhy.sdk.http.ApiAsyncTask;
import com.jmhy.sdk.utils.DisplayUtil;
import com.jmhy.sdk.utils.Logger;
import com.jmhy.sdk.utils.SecurityUtils;
import com.jmhy.sdk.utils.Utils;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;


public class FloatView extends FrameLayout implements OnTouchListener {
    private final static String TAG = FloatView.class.getSimpleName();

    private final int HANDLER_TYPE_HIDE_LOGO = 150;// 隐藏LOGO
    private final int SHOW_KEFU_FLOAT = 160;// 显示客服小红点

    private WindowManager.LayoutParams mWmParams, mWmParams2;
    private WindowManager mWindowManager;
    private static Context mContext;

    private ImageView mIvFloatLogo;
    private LinearLayout mLlFloatMenu;
    private View mTvAccount;
    private View mTvkefu, mTvkefuLine;
    private View mTvGift, mTvGiftLine;
    private View accountTip;
    private View giftTip;
    private View iconTip;
    private View kefuTip;


    private FrameLayout mFlFloatLogo;

    private boolean mIsRight = false;// logo是否在右边
    private boolean mCanHide;// 是否允许隐藏
    private float mTouchStartX;
    private float mTouchStartY;
    private int mScreenWidth;
    private int mScreenHeight;
    private boolean mDraging;
    private boolean mShowLoader = true;

    public final static int INDEX_ACCOUNT = 0x01;
    public final static int INDEX_GIFT = 0x02;
    public final static int INDEX_KEFU = 0x03;

    private final static int smallWidth = 30;

    private final static int red_smallWidth = 56;
    private final static int fullWidth = 56;

    private Timer mTimer, getStateTimer;
    private TimerTask mTimerTask, getStateTAsk;
    private static ApiAsyncTask loginouttask;

    FloatUserInfoView floatUserInfoActivity;
    private boolean isShowUserInfo = false;
    private ObjectAnimator animator = null;

    private View rootFloatView;
    private View hideView;
    private View float_hide;
    private View float_hide_top;
    private boolean isAllowClosed = false;
    private boolean showDialog = false;

    private File file = null;


    Handler mTimerHandler = new Handler(Looper.getMainLooper()) {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HANDLER_TYPE_HIDE_LOGO:
                    // 比如隐藏悬浮框
                    if (mCanHide) {
                        Log.i(TAG, "隐藏悬浮框");

                        mCanHide = false;
                        /*if (mIsRight) {
                            // 靠边隐藏图片
                            mIvFloatLogo.setImageResource(AppConfig.resourceId(mContext, "jm_float_on", "drawable"));
                        } else {
                            mIvFloatLogo.setImageResource(AppConfig.resourceId(mContext, "jm_float_on", "drawable"));
                        }*/

                        setStandbyFloatLogo();

                        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mFlFloatLogo.getLayoutParams();
//                        if (AppConfig.skin == 7 || AppConfig.skin == 8){
                        layoutParams.width = DisplayUtil.dip2px(getContext(), red_smallWidth);
//                        }else{
//                            layoutParams.width = DisplayUtil.dip2px(getContext(), red_smallWidth);
//                        }


//                        Log.i(TAG, "hidden");

//                        refreshIconTip();

                        mWmParams.alpha = 0.7f;


                        if (mContext == null) {
                            Log.i(TAG, "mContext is null");
                        } else {
                            Log.i(TAG, "mContext is not null");

                            mWindowManager.updateViewLayout(FloatView.this, mWmParams);
                            refreshFloatMenu(mIsRight);
                            mLlFloatMenu.setVisibility(View.GONE);
                        }

                    }
                    break;
                case SHOW_KEFU_FLOAT:
                    //显示客服小红点
                    iconTip.setVisibility(VISIBLE);
//                    kefuTip.setVisibility(VISIBLE);
                    Log.i("测试日志", "轮询，显示toast");
                    //Toast.makeText(mContext, "您有新的客服消息，请前往悬浮窗查看", Toast.LENGTH_SHORT).show();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    public FloatView(Context context) {
        super(context);
        init(context);
    }

    public void setRedDotState() {
        Message message = mTimerHandler.obtainMessage();
        message.what = SHOW_KEFU_FLOAT;
        mTimerHandler.sendMessage(message);
    }

    private void init(Context context) {
        this.mContext = context;

        //mWindowManager = ((Activity) mContext).getWindowManager();
        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        // 更新浮动窗口位置参数 靠边
        DisplayMetrics dm = new DisplayMetrics();
        // 获取屏幕信息
        mWindowManager.getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;
        mScreenHeight = dm.heightPixels;
        this.mWmParams = new WindowManager.LayoutParams();
        // 设置window type
        //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        mWmParams.type = WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
        //} else {
        //	mWmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        //}
        // 设置图片格式，效果为背景透明
        mWmParams.format = PixelFormat.RGBA_8888;
        // 设置浮动窗口不可聚焦（实现操作除浮动窗口外的其他可见窗口的操作）
        mWmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        // 调整悬浮窗显示的停靠位置为左侧置?
        mWmParams.gravity = Gravity.LEFT | Gravity.TOP;

        mScreenHeight = mWindowManager.getDefaultDisplay().getHeight();

        // 以屏幕左上角为原点，设置x、y初始值，相对于gravity
        mWmParams.x = 0;
        mWmParams.y = mScreenHeight / 7;

        // 设置悬浮窗口长宽数据
        mWmParams.width = LayoutParams.WRAP_CONTENT;
        mWmParams.height = LayoutParams.WRAP_CONTENT;
        try {

            if (AppConfig.skin == 7 || AppConfig.skin == 8) {
                if (isAllowClosed) {
                    this.mWmParams2 = new WindowManager.LayoutParams();
                    this.mWmParams2.format = PixelFormat.RGBA_8888;
                    this.mWmParams2.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
                    this.mWmParams2.width = LayoutParams.WRAP_CONTENT;
                    this.mWmParams2.height = LayoutParams.WRAP_CONTENT;
                    this.mWmParams2.gravity = Gravity.RIGHT | Gravity.BOTTOM;
                    this.mWindowManager.addView(createHideView(mContext), this.mWmParams2);
                    this.hideView.setVisibility(View.INVISIBLE);
                }

            }

            addView(createView(mContext));
            mWindowManager.addView(this, mWmParams);

        } catch (Exception e) {
            Log.w(TAG, e);
        }
        mTimer = new Timer();
        // hide();

        timerForHide();
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // 更新浮动窗口位置参数 靠边
        DisplayMetrics dm = new DisplayMetrics();
        // 获取屏幕信息
        mWindowManager.getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;
        mScreenHeight = dm.heightPixels;
        int oldX = mWmParams.x;
        int oldY = mWmParams.y;
        switch (newConfig.orientation) {
            case Configuration.ORIENTATION_LANDSCAPE:// 横屏
		/*	if (mIsRight) {
				mWmParams.x = mScreenWidth;
				mWmParams.y = oldY;
			} else {*/
                mWmParams.x = oldX;
                mWmParams.y = oldY;
                //}
                break;
            case Configuration.ORIENTATION_PORTRAIT:// 竖屏
			/*if (mIsRight) {
				mWmParams.x = mScreenWidth;
				mWmParams.y = oldY;
			} else {*/
                mWmParams.x = oldX;
                mWmParams.y = oldY;
                //}
                break;
        }
        mWindowManager.updateViewLayout(this, mWmParams);
    }

    /**
     * 创建Float view
     *
     * @param context
     * @return
     */
    private View createView(final Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        // 从布局文件获取浮动窗口视图

        Log.i(TAG, "AppConfig.skin == " + AppConfig.skin);

        switch (AppConfig.skin) {
            case 7:
                rootFloatView = inflater.inflate(
                        AppConfig.resourceId(context, "jm_float_view_red", "layout"), null);
                break;
            case 8:
                rootFloatView = inflater.inflate(
                        AppConfig.resourceId(context, "jm_float_view_red", "layout"), null);
                break;
            case 6:
            case 5:
            case 4:
                rootFloatView = inflater.inflate(
                        AppConfig.resourceId(context, "jm_float_view_4", "layout"), null);
                break;
            default:
                rootFloatView = inflater.inflate(
                        AppConfig.resourceId(context, "jm_float_view", "layout"), null);
        }
        mFlFloatLogo = (FrameLayout) rootFloatView.findViewById(AppConfig.resourceId(context, "float_view", "id"));
        // 浮点图标
        mIvFloatLogo = (ImageView) rootFloatView.findViewById(AppConfig.resourceId(context, "float_view_icon_imageView", "id"));

        mLlFloatMenu = (LinearLayout) rootFloatView.findViewById(AppConfig.resourceId(context, "ll_menu", "id"));
        mTvAccount = rootFloatView.findViewById(AppConfig
                .resourceId(context, "tv_account", "id"));
        mTvAccount.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                turnTouserIntent(AppConfig.USERURL);
                mLlFloatMenu.setVisibility(View.GONE);
//                hiddenTip(INDEX_ACCOUNT);
            }
        });

        mTvGift = rootFloatView.findViewById(AppConfig
                .resourceId(context, "tv_gift", "id"));
        mTvGift.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                turnToIntent(AppConfig.GIFT);
                mLlFloatMenu.setVisibility(View.GONE);
//                hiddenTip(INDEX_GIFT);
            }
        });

        mTvkefu = rootFloatView.findViewById(AppConfig
                .resourceId(context, "tv_kefu", "id"));
        mTvkefu.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                turnToIntent(AppConfig.KEFU);
//                hiddenTip(INDEX_KEFU);
            }
        });

        accountTip = rootFloatView.findViewById(AppConfig
                .resourceId(context, "account_tip", "id"));
        giftTip = rootFloatView.findViewById(AppConfig
                .resourceId(context, "gift_tip", "id"));
        kefuTip = rootFloatView.findViewById(AppConfig
                .resourceId(context, "kefu_tip", "id"));
        iconTip = rootFloatView.findViewById(AppConfig
                .resourceId(context, "icon_tip", "id"));
        mTvkefuLine = rootFloatView.findViewById(AppConfig
                .resourceId(context, "tv_kefu_line", "id"));
        mTvGiftLine = rootFloatView.findViewById(AppConfig
                .resourceId(context, "tv_gift_line", "id"));

        rootFloatView.setOnTouchListener(this);
        rootFloatView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mDraging) {
                    Utils.getSeferencegameuser(mContext);
                    if (AppConfig.skin == 7 || AppConfig.skin == 8) {
                        FloatView.this.mCanHide = true;
                        FloatView.this.isShowUserInfo = true;
                        FloatView.this.timerForHide();


                        FloatView.this.turnToIntent(AppConfig.GIFT);

                    } else if (mLlFloatMenu.getVisibility() == View.VISIBLE) {
                        mLlFloatMenu.setVisibility(View.GONE);
                    } else {
                        mLlFloatMenu.setVisibility(View.VISIBLE);
                    }
                }
            }
        });

        rootFloatView.measure(View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
                .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        /*if (AppConfig.is_user_float_on.equals("0")) {
            mTvAccount.setVisibility(View.INVISIBLE);
        }
        if (AppConfig.is_service_float_on.equals("0")) {
            mTvkefu.setVisibility(View.INVISIBLE);
        }*/

        if (TextUtils.isEmpty(AppConfig.USERURL)) {
            mTvAccount.setVisibility(GONE);
        }

        if (TextUtils.isEmpty(AppConfig.KEFU)) {
            mTvkefu.setVisibility(GONE);
            mTvkefuLine.setVisibility(GONE);
        } else {
            if (mTvAccount.getVisibility() == GONE) {
                mTvkefuLine.setVisibility(GONE);
            }
        }

        if (TextUtils.isEmpty(AppConfig.GIFT)) {
            mTvGift.setVisibility(GONE);
            mTvGiftLine.setVisibility(GONE);
        } else {
            if (mTvkefu.getVisibility() == GONE) {
                mTvGiftLine.setVisibility(GONE);
            }
        }

        setFloatLogo();

        if (AppConfig.showFloatRedTip) {
            setRedDotState();
        }


//        refreshIconTip();

        return rootFloatView;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (mContext == null) {
            Log.e("JiMiSDK", "floatview mcontext is null");
        }
        removeTimerTask();
        // 获取相对屏幕的坐标，即以屏幕左上角为原点
        int x = (int) event.getRawX();
        int y = (int) event.getRawY();

        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mFlFloatLogo.getLayoutParams();
        layoutParams.width = DisplayUtil.dip2px(getContext(), fullWidth);


        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTouchStartX = event.getX();
                mTouchStartY = event.getY();

                setFloatLogo();

                iconTip.setVisibility(GONE);

                mWmParams.alpha = 1f;
                mWindowManager.updateViewLayout(this, mWmParams);
                mDraging = false;
                break;
            case MotionEvent.ACTION_MOVE:
                float mMoveStartX = event.getX();
                float mMoveStartY = event.getY();

                // 如果移动量大于3才移动
                if (Math.abs(mTouchStartX - mMoveStartX) > 3
                        && Math.abs(mTouchStartY - mMoveStartY) > 3) {

                    if (this.hideView != null) {
                        Log.i("jimi", "区域设置显示");
                        this.hideView.setVisibility(View.VISIBLE);
                    }


                    mDraging = true;
                    // 更新浮动窗口位置参数
                    mWmParams.x = (int) (x - mTouchStartX);
                    mWmParams.y = (int) (y - mTouchStartY);

                    setFloatLogo();

//                    iconTip.setVisibility(GONE);
                    if (AppConfig.skin == 7 || AppConfig.skin == 8) {
                        if (isAllowClosed) {


                            this.mWmParams.alpha = 1.0f;
                            int cententFloatWidth = this.mFlFloatLogo.getWidth();
                            int cententFloatHeight = this.mFlFloatLogo.getHeight();
                            int minWidth = (this.mScreenWidth - this.float_hide.getWidth()) + cententFloatWidth;
                            int minHeight = (this.mScreenHeight - this.float_hide.getHeight()) + cententFloatHeight;
                            if (this.mWmParams.x + cententFloatWidth >= minWidth && this.mWmParams.y + cententFloatHeight >= minHeight) {


                                this.float_hide_top.setVisibility(View.VISIBLE);
                                this.showDialog = true;


                            } else if ((this.mWmParams.x + cententFloatWidth < minWidth || this.mWmParams.y + cententFloatHeight < minHeight)) {

                                this.showDialog = false;

                                this.float_hide_top.setVisibility(View.INVISIBLE);
                            }
                        }
                    }
                    mWmParams.alpha = 0.7f;
                    mWindowManager.updateViewLayout(this, mWmParams);
                    mLlFloatMenu.setVisibility(View.GONE);
                    return false;
                }

                break;
            case MotionEvent.ACTION_UP:
                FloatView.this.cancelMethod();
                if (this.hideView != null) {
                    Log.i("jimi", "区域设置隐藏");
                    this.hideView.setVisibility(View.INVISIBLE);
                }
                if (showDialog) {
                    showDialog = false;
                    if (this.float_hide_top != null) {
                        this.float_hide_top.setVisibility(View.INVISIBLE);
                    }
                    new HideFloatDialog(getContext(), AppConfig.resourceId(getContext(), "jm_MyDialog", "style"), new HideFloatDialog.HideDialogListener() {
                        public void onConfirm() {
                            FloatView.this.isShowUserInfo = true;
                            FloatView.this.hide();
                        }

                        public void onCancel() {
                            FloatView.this.mCanHide = true;

                        }
                    }).show();


                }
                break;
            case MotionEvent.ACTION_CANCEL:


                break;
        }

        return false;
    }

    private void cancelMethod() {
        if (mWmParams.x >= mScreenWidth / 2) {
            mWmParams.x = 0;
            mIsRight = false;
        } else if (mWmParams.x < mScreenWidth / 2) {
            mIsRight = false;
            mWmParams.x = 0;
        }

        setFloatLogo();

        iconTip.setVisibility(GONE);

        mWmParams.alpha = 1f;
        refreshFloatMenu(mIsRight);
        timerForHide();
        mWindowManager.updateViewLayout(this, mWmParams);
        // 初始化
        mTouchStartX = mTouchStartY = 0;
    }

    private void removeTimerTask() {
        if (mTimerTask != null) {
            mTimerTask.cancel();
            mTimerTask = null;
        }
    }

    private void removeSocketTimerTask() {
        if (getStateTAsk != null) {
            getStateTAsk.cancel();
            getStateTAsk = null;
        }
    }

    private void removeFloatView() {
        try {
      /*      mWmParams.alpha = 0;
            mWindowManager.updateViewLayout(this, mWmParams);*/
            Log.e("JiMiSDK", "floatview removeFloatView");

            mWindowManager.removeView(this);
            this.mWindowManager.removeView(this.hideView);

            mWindowManager = null;
            removeAllViews();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 隐藏悬浮窗
     */
    public void hide() {
        setVisibility(View.GONE);
        Message message = mTimerHandler.obtainMessage();
        message.what = HANDLER_TYPE_HIDE_LOGO;
        mTimerHandler.sendMessage(message);
        removeTimerTask();
        removeSocketTimerTask();
    }

    /**
     * 显示悬浮窗
     */
    public void show() {
        Log.i(TAG, "显示悬浮窗...");

        try {
            if (getVisibility() != View.VISIBLE) {
                setVisibility(View.VISIBLE);

                if (mShowLoader) {
                    FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mFlFloatLogo.getLayoutParams();
                    layoutParams.width = DisplayUtil.dip2px(getContext(), fullWidth);
                    Log.i(TAG, "show");

                    setFloatLogo();

                    iconTip.setVisibility(GONE);

                    mWmParams.alpha = 1f;
                    mWindowManager.updateViewLayout(this, mWmParams);

                    timerForHide();

                    mShowLoader = false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 刷新float view menu
     *
     * @param right
     */
    private void refreshFloatMenu(boolean right) {

        if (right) {
            FrameLayout.LayoutParams paramsFloatImage = (FrameLayout.LayoutParams) mIvFloatLogo
                    .getLayoutParams();
            paramsFloatImage.gravity = Gravity.RIGHT;

            FrameLayout.LayoutParams paramsFlFloat = (FrameLayout.LayoutParams) mFlFloatLogo
                    .getLayoutParams();
            paramsFlFloat.gravity = Gravity.RIGHT;


        } else {
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mIvFloatLogo
                    .getLayoutParams();

            params.gravity = Gravity.LEFT;

            FrameLayout.LayoutParams paramsFlFloat = (FrameLayout.LayoutParams) mFlFloatLogo
                    .getLayoutParams();
            paramsFlFloat.gravity = Gravity.LEFT;


        }
    }

    /**
     * 定时隐藏float view
     */
    private void timerForHide() {
        Log.i(TAG, "timerForHide");

        mCanHide = true;

        // 结束任务
        if (mTimerTask != null) {
            try {
                mTimerTask.cancel();
                mTimerTask = null;
            } catch (Exception e) {
            }

        }
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                Message message = mTimerHandler.obtainMessage();
                message.what = HANDLER_TYPE_HIDE_LOGO;
                mTimerHandler.sendMessage(message);
            }
        };
        if (mCanHide) {
            mTimer.schedule(mTimerTask, 6000, 3000);
        }
    }

    public void notifyWebUpfileStatus(int requestCode, int resultCode, Intent data) {
        if (this.floatUserInfoActivity != null) {
            this.floatUserInfoActivity.notifyWebUpfileStatus(requestCode, resultCode, data);
        }
    }

    public void destroy() {
        Log.e("JiMiSDK", "floatview destroy");
        mContext = null;
        removeTimerTask();
        mTimer.cancel();
        mTimer = null;

        if (getStateTimer != null) {
            getStateTimer.cancel();
            getStateTimer = null;
        }

        mTimerHandler.removeCallbacksAndMessages(null);
        mTimerHandler = null;

        if (getStateTAsk != null) {
            getStateTAsk.cancel();
            getStateTAsk = null;
        }

        removeFloatView();
        this.float_hide_top = null;
    }

    private void turnToIntent(String url) {
        Logger.d("111111");
        Logger.d("url 111 : " + url);
        JiMiSDK.reportUserAction(mContext, "sdk_open_float");

        if (TextUtils.isEmpty(url)) {
            String tip = AppConfig.getString(mContext, "function_not_open");
            Toast.makeText(mContext, tip, Toast.LENGTH_SHORT).show();
            return;
        }
        /**
         Intent intent = new Intent();
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
         | Intent.FLAG_ACTIVITY_SINGLE_TOP);
         intent.putExtra("url", url);
         intent.putExtra("isKefu", true);
         intent.setClass(mContext, JmGiftActivity.class);
         mContext.startActivity(intent);
         **/

        if (this.floatUserInfoActivity == null) {
            this.floatUserInfoActivity = new FloatUserInfoView((Activity) mContext, new FloatUserInfoView.CloseFloatListener() {
                public void closeFloat() {
                    FloatView.this.isShowUserInfo = false;
                    FloatView.this.show();
                }
            });
            this.floatUserInfoActivity.setViews(url);
            this.floatUserInfoActivity.show();
        } else {
            this.floatUserInfoActivity.setViews(url);
            this.floatUserInfoActivity.show();
        }

        hide();


    }

    private void turnTouserIntent(String url) {
        if (TextUtils.isEmpty(url)) {
            String tip = AppConfig.getString(mContext, "function_not_open");
            Toast.makeText(mContext, tip, Toast.LENGTH_SHORT).show();
            return;
        }

        Intent intent = new Intent();
		/*intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_SINGLE_TOP);*/
        intent.putExtra("url", url);
        intent.setClass(mContext, JmCommunityActivity.class);
        mContext.startActivity(intent);

    }

    public void hiddenTip(int index) {
        switch (index) {
            case INDEX_ACCOUNT:
                accountTip.setVisibility(GONE);
                break;
            case INDEX_GIFT:
                giftTip.setVisibility(GONE);
                break;
            case INDEX_KEFU:
                kefuTip.setVisibility(GONE);
                break;
        }

        //       refreshIconTip();
    }

    private void refreshIconTip() {
        if (accountTip.getVisibility() == View.VISIBLE || giftTip.getVisibility() == View.VISIBLE || kefuTip.getVisibility() == View.VISIBLE) {
            iconTip.setVisibility(VISIBLE);
        } else {
            iconTip.setVisibility(GONE);
        }
    }

    public void showTip(int index) {
        switch (index) {
            case INDEX_ACCOUNT:
                accountTip.setVisibility(VISIBLE);
                break;
            case INDEX_GIFT:
                giftTip.setVisibility(VISIBLE);
                break;
            case INDEX_KEFU:
                kefuTip.setVisibility(VISIBLE);
                break;
        }

        refreshIconTip();
    }


    private View createHideView(Context context) {
        this.hideView = LayoutInflater.from(context).inflate(AppConfig.resourceId(context, "jm_float_red_hide", "layout"), null);
        this.float_hide_top = this.hideView.findViewById(AppConfig.resourceId(context, "float_hide_top", "id"));
        this.float_hide = this.hideView.findViewById(AppConfig.resourceId(context, "float_hide", "id"));
        return this.hideView;
    }

    private void setFloatLogo() {
        setIconImg(false);

    }


    private void setStandbyFloatLogo() {
        setIconImg(true);

    }

    private void setIconImg(boolean isStandby) {

        if (isStandby) {
            if (isExistsIconImg(AppConfig.floatHalfIconUrl)) {

                Glide.with(getContext()).load(this.file).dontTransform().into(this.mIvFloatLogo);

                if (iconTip.getVisibility() == VISIBLE) {
                    setLayoutX(iconTip, mIvFloatLogo.getWidth() / 3);
                }


                this.file = null;
            } else {
                if (AppConfig.float_gift.equals("1")) {
                    Glide.with(mContext).load(AppConfig.resourceId(mContext,
                            "jm_gif_test", "drawable")).asGif().dontTransform().into(mIvFloatLogo);
                } else {
                    mIvFloatLogo.setImageResource(AppConfig.resourceId(mContext,
                            "jm_float_on", "drawable"));
                }

            }
        } else {
            if (isExistsIconImg(AppConfig.floatIconUrl)) {
                Glide.with(getContext()).load(this.file).dontTransform().into(this.mIvFloatLogo);
                this.file = null;
            } else {
                if (AppConfig.float_gift.equals("1")) {
                    Glide.with(mContext).load(AppConfig.resourceId(mContext,
                            "jm_gif_test", "drawable")).asGif().dontTransform().into(mIvFloatLogo);
                } else {
                    mIvFloatLogo.setImageResource(AppConfig.resourceId(mContext,
                            "jm_float", "drawable"));
                }
            }
        }


    }

    private boolean isExistsIconImg(String url) {
        if (url == null || url.equals("")) {
            return false;
        }
        String md5ResultString = SecurityUtils.getMD5Str(url);
        if (this.file == null) {
            if (Build.VERSION.SDK_INT < 29) {
                this.file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera");
            } else {
                this.file = JiMiSDK.mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
            }
        }
        this.file = new File(this.file + "/" + md5ResultString + ".gif");
        if (!this.file.exists()) {
            return false;
        }

        return true;
    }

    public void setLayoutX(View view, int x) {
        MarginLayoutParams margin = new MarginLayoutParams(view.getLayoutParams());
        margin.setMargins(x, margin.topMargin, margin.width, margin.height);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(margin);
        view.setLayoutParams(layoutParams);

    }

}

